package l1j.opqlo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import l1j.opqlo.NewSystem.PlaceReLoad;
import l1j.server.DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

//opqlo 伺服器排名
public class Place {

	private static Place _instance;

	/** 0:全職業排行 1:王子排行 2:騎士排行 3:妖精排行 4:法師排行 5:黑妖排行 6:龍騎士排行 7:幻術士排行 */
	private static final HashMap<Integer, String[]> Prince = new HashMap<Integer, String[]>();
	private static final HashMap<Integer, String> Type = new HashMap<Integer, String>();
	/*
	 * private static String[] Prince0;//全職業排行 by opqlo private static String[]
	 * Prince1;//王子排行 by opqlo private static String[] Prince2;//騎士排行 by opqlo
	 * private static String[] Prince3;//妖精排行 by opqlo private static String[]
	 * Prince4;//法師排行 by opqlo private static String[] Prince5;//黑妖排行 by opqlo
	 * private static String[] Prince6;//龍騎士排行 by opqlo private static String[]
	 * Prince7;//幻術士排行 by opqlo
	 */
	private static final StringBuilder opqlo = new StringBuilder();
	private static Connection con;
	public static String 空缺 = "空缺";

	public static Place getInstance() {
		if (_instance == null) {

			_instance = new Place();
			new PlaceReLoad(3600).begin();

			Type.put(new Integer(99),
					"select char_name,Clanname from characters where accesslevel = 0 ");
			Type.put(new Integer(1),
					"&& type = 0 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(2),
					"&& type = 1 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(3),
					"&& type = 2 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(4),
					"&& type = 3 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(5),
					"&& type = 4 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(6),
					"&& type = 5 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(7),
					"&& type = 6 order by metempsychosis DESC, exp DESC ");
			Type.put(new Integer(0), "order by metempsychosis DESC, exp DESC ");
			System.out.println("※-----自動更新排名【啟動】-----※");
		}
		return _instance;
	}

	public String[] getPrince(final int i) {
		return Prince.get(i);
	}

	public void load() throws SQLException, InterruptedException {
		this.Prince();
	}

	private void Prince() throws SQLException {
		Prince.clear();
		for (int i = 0; i < 8; i++) {
			String[] values = new String[21];
			opqlo.append(Type.get(99)).append(Type.get(i));
			/*
			 * switch(i){ case 1:
			 * opqlo.append("&& type = 0 order by metempsychosis DESC, exp DESC "
			 * );break; case 2:
			 * opqlo.append("&& type = 1 order by metempsychosis DESC, exp DESC "
			 * );break; case 3:
			 * opqlo.append("&& type = 2 order by metempsychosis DESC, exp DESC "
			 * );break; case 4:
			 * opqlo.append("&& type = 3 order by metempsychosis DESC, exp DESC "
			 * );break; case 5:
			 * opqlo.append("&& type = 4 order by metempsychosis DESC, exp DESC "
			 * );break; case 6:
			 * opqlo.append("&& type = 5 order by metempsychosis DESC, exp DESC "
			 * );break; case 7:
			 * opqlo.append("&& type = 6 order by metempsychosis DESC, exp DESC "
			 * );break; default:
			 * opqlo.append("order by metempsychosis DESC, exp DESC "); }
			 */

			con = DatabaseFactory.get().getConnection();

			// (以下是MYSQL搜尋排序的指令，主要內容有：GM不列入排序、篩選出王族男女CLASSID、經驗值由大到小排列。因為等級是依照經驗值來計算的，
			// 所以不搜尋等級，而搜尋經驗值，同等級經驗值高者名次較前。最後是從第一筆往下取一筆資料、從第二筆往下取一筆資料這樣。)
			final ResultSet pstm1 = con.prepareStatement(
					opqlo.toString() + "limit 0,1;").executeQuery(), pstm2 = con
					.prepareStatement(opqlo.toString() + "limit 1,1;")
					.executeQuery(), pstm3 = con.prepareStatement(
					opqlo.toString() + "limit 2,1;").executeQuery(), pstm4 = con
					.prepareStatement(opqlo.toString() + "limit 3,1;")
					.executeQuery(), pstm5 = con.prepareStatement(
					opqlo.toString() + "limit 4,1;").executeQuery(), pstm6 = con
					.prepareStatement(opqlo.toString() + "limit 5,1;")
					.executeQuery(), pstm7 = con.prepareStatement(
					opqlo.toString() + "limit 6,1;").executeQuery(), pstm8 = con
					.prepareStatement(opqlo.toString() + "limit 7,1;")
					.executeQuery(), pstm9 = con.prepareStatement(
					opqlo.toString() + "limit 8,1;").executeQuery(), pstm10 = con
					.prepareStatement(opqlo.toString() + "limit 9,1;")
					.executeQuery();
			opqlo.delete(0, opqlo.length());
			// (不懂SQL指令的話，請自行查閱SQL的說明文件)
			// 接著是取得玩家的名字

			if (pstm1.next()) {
				values[1] = pstm1.getString("char_name");
				values[11] = pstm1.getString("Clanname");
			} else {
				values[1] = 空缺;
				values[11] = 空缺;
			}
			SQLUtil.close(pstm1);
			if (pstm2.next()) {
				values[2] = pstm2.getString("char_name");
				;
				values[12] = pstm2.getString("Clanname");
			} else {
				values[2] = 空缺;
				values[12] = 空缺;
			}
			SQLUtil.close(pstm2);

			if (pstm3.next()) {
				values[3] = pstm3.getString("char_name");
				values[13] = pstm3.getString("Clanname");
			} else {
				values[3] = 空缺;
				values[13] = 空缺;
			}
			SQLUtil.close(pstm3);

			if (pstm4.next()) {
				values[4] = pstm4.getString("char_name");
				values[14] = pstm4.getString("Clanname");
			} else {
				values[4] = 空缺;
				values[14] = 空缺;
			}
			SQLUtil.close(pstm4);

			if (pstm5.next()) {
				values[5] = pstm5.getString("char_name");
				values[15] = pstm5.getString("Clanname");
			} else {
				values[5] = 空缺;
				values[15] = 空缺;
			}
			SQLUtil.close(pstm5);

			if (pstm6.next()) {
				values[6] = pstm6.getString("char_name");
				values[16] = pstm6.getString("Clanname");
			} else {
				values[6] = 空缺;
				values[16] = 空缺;
			}
			SQLUtil.close(pstm6);

			if (pstm7.next()) {
				values[7] = pstm7.getString("char_name");
				values[17] = pstm7.getString("Clanname");
			} else {
				values[7] = 空缺;
				values[17] = 空缺;
			}
			SQLUtil.close(pstm7);

			if (pstm8.next()) {
				values[8] = pstm8.getString("char_name");
				values[18] = pstm8.getString("Clanname");
			} else {
				values[8] = 空缺;
				values[18] = 空缺;
			}
			SQLUtil.close(pstm8);

			if (pstm9.next()) {
				values[9] = pstm9.getString("char_name");
				values[19] = pstm9.getString("Clanname");
			} else {
				values[9] = 空缺;
				values[19] = 空缺;
			}
			SQLUtil.close(pstm9);

			if (pstm10.next()) {
				values[10] = pstm10.getString("char_name");
				values[20] = pstm10.getString("Clanname");
			} else {
				values[10] = 空缺;
				values[20] = 空缺;
			}
			SQLUtil.close(pstm10);
			SQLUtil.close(con); // (最後記得關閉連線，不然sql指令的佇列累積太多會當掉。)

			Prince.put(i, values);
			values = null;
			try {
				Thread.sleep(10);
			} catch (final InterruptedException e) {
				e.printStackTrace();
				continue;
			}
			// levelresult = null;
		}

	}

}
